#include "StringScreen.h"
#include <fstream>
#include <iostream>
#include "NGLog.h"
#include "String.h"

using namespace std;


StringScreenMgr::StringScreenMgr()
{
	m_bType = true;
	LoadDenyWordsTxt();
}

// 重载过滤文字配置
bool StringScreenMgr::LoadDenyWordsTxt()
{
	m_dirVec.clear();
	
	FILE* pfile = fopen("../scripts_xml/deny_words.txt", "r");
	ASSERT(pfile);
	
	const int num = 50;
	char buffer[num] = {0};
	char newbuffer[num] = {0};
	int conuntNum = 0;
	String str;
	uint32 Val = 0;
	
	while(fgets(buffer, num, pfile))
	{
		//去掉换行
		conuntNum = 0;
		while(buffer[conuntNum] != 13)
		{
			newbuffer[conuntNum] = buffer[conuntNum];
			++conuntNum;
		}
		newbuffer[conuntNum] = 0;
		
		//加入头字符
		Val = 0;
		uint8 byteNum = ByteNum_UTF8(newbuffer[0]);
		for(int i = 0; i < byteNum; ++i)
		{
			Val = Val << 8;
			Val += newbuffer[i];
		}
		
		str = newbuffer;
		//找出是否存在该头字符	
		UintStrDir::iterator itr = m_dirVec.find(Val);
		//没有则加入字典
		if(itr == m_dirVec.end())
		{	
			cStrSet* pStrSet = new cStrSet;
			pStrSet->insert(str);
			m_dirVec.insert(m_dirVec.begin(), UintStr_Pair(Val, pStrSet));
		}
		//有则加入字典对应的位置
		else
		{
			cStrSet::iterator setIt = itr->second->find(str);
			if(setIt == itr->second->end())
				itr->second->insert(str);
		}
		
		//清除BUFFER
		memset(buffer, 0, num);
		memset(newbuffer, 0, num);
	}
	
	fclose(pfile);
	return true;
}

StringScreenMgr::~StringScreenMgr()
{
	for(UintStrDir::iterator itr = m_dirVec.begin(); itr != m_dirVec.end(); ++itr)
		delete itr->second;
}

bool StringScreenMgr::StringConverter_Ext(String& str)
{
	if(str.empty())
		return false;
	//检查是否为utf-8编码
	if(!StringUtil::checkStringUTF8(str))
		StringUtil::StringToUTF8(str);
		
	bool bCover = false;
	uint32 val = 0;
	UintStrDir::iterator itr, itr_end = m_dirVec.end();
	String::iterator strIt = str.begin(), strItend = str.end(), strTempIt;
	basic_string <char>::size_type offpos = 0;
	
	for(; strIt != strItend; )
	{
		val = 0;
		strTempIt = strIt;
		//查找其为utf-8中的第几位
		unsigned char byteNum = ByteNum_UTF8(*strIt);
		
		for(int i = 0; i < byteNum; ++i)
		{
			val = val << 8;
			val += *(strTempIt++);
		}
		
		bool match = true;
		//找出在字典中是否有该头字符
		itr = m_dirVec.find(val);

		if(itr != itr_end)
		{
			//反向遍历
			for(cStrSet::reverse_iterator setItr = itr->second->rbegin(), setItr_end = itr->second->rend();
				setItr != setItr_end; ++setItr)
			{
				uint32 count = (*setItr).length();

				basic_string <char>::size_type pos = offpos, endPos = offpos + count;
				match = true;
				//是否匹配
				for(size_t k = 0; pos < endPos; ++pos, ++k)
				{
					if(str[pos] != (*setItr)[k])
					{
						match = false;
						break;
					}
				}
				
				if(match)
				{
					bCover = true;
					//和谐为火星文
					for(size_t i = 0; i < count; ++i)
						*(strIt++) = '*';
					//跳转位置
					offpos += count;
					break;
				}
			}
			
			if(match)
				continue;
		}
		
		strIt += byteNum;
		offpos += byteNum;
	}
	return bCover;
}

uint8 StringScreenMgr::ByteNum_UTF8(const uint8& chr)
{
	//这个别问我　就是这个规定的
	if(0x0 == chr >> 7)
		return 1;
	else if(0x6 == chr >> 5)
		return 2;
	else if(0xE == chr >> 4)
		return 3;
	else if(0x1E == chr >> 3)
		return 4;
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////
